package com.cn.lyl.servlet.lawyer;

import com.alibaba.fastjson.JSONObject;
import com.cn.lyl.model.LawyerModel;
import com.cn.lyl.model.Page;
import com.cn.lyl.model.UserModel;
import com.cn.lyl.service.LawyerService;
import com.cn.lyl.service.UserService;
import com.cn.lyl.service.impl.LawyerServiceImpl;
import com.cn.lyl.service.impl.UserServiceImpl;
import com.cn.lyl.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/back/lawyer/list")
public class FindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //        1.得到前端传递过来的数据

        String lawyerName = req.getParameter("lawyer");

        String role = req.getParameter("role");
        String isHome = req.getParameter("isHome");
        LawyerModel lawyerModel = new LawyerModel();
        lawyerModel.setLawyerName(lawyerName);
        lawyerModel.setDependenceObj(role);
        Page page=new Page();
        page.setPageCurr(Tool.curr(req.getParameter("page")));
        page.setPageSize(Tool.size(req.getParameter("limit")));
        lawyerModel.setPage(page);
        lawyerModel.setIsHome(isHome);
        LawyerService lawyerService=new LawyerServiceImpl();
        List<LawyerModel> lawyerModels = lawyerService.findAllLawyer(lawyerModel);
        List<LawyerModel> changeImgs = new ArrayList<>();
        for(LawyerModel changeImg:lawyerModels){
            changeImg.setHead("<img src='"+changeImg.getHead()+"'>");
            changeImgs.add(changeImg);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",lawyerService.getCount(lawyerModel));
        jsonObject.put("data",changeImgs);
        resp.getWriter().print(jsonObject);
    }
}
